package com.haixiaoke.flat.lock.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.auth.AuthCheck;
import com.haixiaoke.saas.common.config.DevicesConfig;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.houseMember.domain.HouseMember;
import com.haixiaoke.saas.houseMember.service.IHouseMemberService;
import com.haixiaoke.saas.lock.domain.HouseLock;
import com.haixiaoke.saas.lock.domain.LockPwd;
import com.haixiaoke.saas.lock.service.IHouseLockService;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.saas.user.domain.UserConsumer;
import com.haixiaoke.saas.user.service.IUserConsumerService;
import com.haixiaoke.token.FrontTokenComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 房间锁信息Controller
 *
 * @author qixi
 * @date 2024-07-25
 */
@RestController
@RequestMapping("/front/flat/lock/houseLock")
public class FlatHouseLockController extends BaseController {
    @Autowired
    private IHouseLockService houseLockService;

    @Autowired
    private DevicesConfig devicesConfig;

    @Autowired
    private FrontTokenComponent frontTokenComponent;

    @Autowired
    private IUserConsumerService userConsumerService;

    @Autowired
    private IHouseMemberService houseMemberService;

    /**
     * 查询房间锁信息列表
     */
    // @PreAuthorize("@ss.hasPermi('business:houseLock:list')")
    @GetMapping("/list")
    @AuthCheck
    public AjaxResult list(HouseLock houseLock) {
        if (ObjectUtil.isEmpty(houseLock)) {
            throw new ServiceException("参数为空");
        }

        if (StrUtil.isEmpty(houseLock.getHouseId())) {
            throw new ServiceException("房屋id不能为空");
        }

        List<HouseLock> list = houseLockService.selectHouseLockList(houseLock);
        return success(list);
    }


    /**
     * 查询房间临时密码列表
     */
    // @PreAuthorize("@ss.hasPermi('business:houseLock:list')")
    @GetMapping("/pwdList")
    // @AuthCheck
    public TableDataInfo pwdList(HouseLock houseLock) {
        startPage();
        List<LockPwd> list = houseLockService.selectPwdList(houseLock);
        return getDataTable(list);
    }


    /**
     * 新增房间临时密码
     */
    @PostMapping("/addPwd")
    @AuthCheck
    // @Log(title = "房间临时密码", businessType = BusinessType.INSERT)
    public AjaxResult addPwd(@RequestBody @Valid LockPwd lockPwd) {


        UserConsumer userConsumer = userConsumerService.selectUserConsumerByUserId(frontTokenComponent.getUserIdNotNull());
        if (ObjectUtil.isEmpty(userConsumer) || StrUtil.isBlank(userConsumer.getOpenId())) {
            return error("用户数据异常");
        }

        List<HouseMember> houseMembers = houseMemberService.selectHouseMemberListByOrderId(lockPwd.getOrderId());
        if (ObjectUtil.isEmpty(houseMembers)) {
            return error("订单数据异常");
        }

        List<HouseMember> collect = houseMembers.stream().filter(item -> item.getUserId().equals(userConsumer.getUserId())).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(collect)) {
            return error("用户不在订单中");
        }

        lockPwd.setUserId(userConsumer.getUserId());
        lockPwd.setUserName(userConsumer.getUserName());
        return toAjax(houseLockService.insertFlatLockPwd(lockPwd));
    }


}
